# -*- coding: utf-8 -*-
import json

import scrapy
class Item(dict):
    class_name = ''

    def __init__(self, selector, class_name=''):
        super().__init__()
        self._selector = selector
        self.class_name = class_name

    def add_path(self, attr, xpath, begin=0, end=None):
        if end is None:
            self[attr] = self._selector.xpath(xpath).get(begin)
            return
        self[attr] = self._selector.xpath(xpath).getall()[begin:end + 1]
    def add_v(self,key,value):
        self[key]=value
class SpiderCarStyleSpider(scrapy.Spider):
    name = 'spider_car_origin'
    start_urls = ['https://xl.16888.com/style.html']
    base_url='https://xl.16888.com'
    def parse(self, response):

        cur=int(response.xpath('//a[@class="lineBlock num curr"]/text()').extract()[0])
        nums=response.xpath('//a[@class="lineBlock num"]/text()').extract()
        max=cur
        if len(nums)>0:
            max=int(nums.pop())
        while cur<=max:
            url=response.urljoin('style-'+str(cur)+'.html')
            cur+=1
            yield scrapy.Request(url,callback=self.style_solver)

    def style_solver(self,response):
        trs=response.xpath('//table/tr')
        for tr in trs:
            if(len(tr.xpath('td').extract())==0):
                continue
            sale_url=tr.xpath('td//a[@class="lineBlock xl-td-btn"]/@href').extract()[0]
            declare_url=tr.xpath('td//a[@class="lineBlock xl-td-btn"]/@href').extract()[1]
            yield scrapy.Request(response.urljoin(declare_url),callback=self.declare_solver,meta={
                'sale_url':response.urljoin(sale_url)
            })
    def declare_solver(self,response):
        name=response.xpath("//a[@class='J_show']/text()").extract()[0]
        dds=response.xpath('//div[@class="show_box"]/dl')
        str=dds.xpath('dd[1]//b/text()').extract()[0]
        str = str.replace('\r\n', '').replace('万', '').strip().split('-')
        min_price=float(str[0])
        max_price=float(str[1])
        brand=dds.xpath('//dd[3]/span[1]/a/text()').extract()[0]
        type=dds.xpath('//dd[5]/span[2]/em/text()').extract()[0]


        sale_url=response.meta['sale_url']
        return scrapy.Request(sale_url,callback=self.sale_solver,meta={
            'name':name,
            'min_price':min_price,
            'max_price':max_price,
            'brand':brand,
            'type':type,
        })

    def sale_solver(self,response):
        for tr in response.xpath('//table/tr'):
            if len(tr.xpath('td'))==0:
                continue
            item = Item(tr,'car')
            item['name']=response.meta['name']
            item['brand']=response.meta['brand']
            item['type']=response.meta['type']
            item['price']=round((float(response.meta['min_price'])+float(response.meta['max_price']))/2,2)
            item.add_path('date','td[1]/text()')
            item.add_path('sale','td[2]/text()')
            if item['date']==0:
                continue
            item['sale']=int(item['sale'])
            yield  item
        next_url=response.xpath('//a[@class="lineBlock next"]/@href').extract()
        if len(next_url)>0:
            next_url=response.urljoin(next_url[0])
            yield scrapy.Request(
                next_url,
                meta=response.meta,
                callback=self.sale_solver
            )



